package com.jaydenxiao.common.base;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;

import com.gyf.barlibrary.ImmersionBar;
import com.jaydenxiao.common.R;

/**
 * Created by hasee on 2017/10/7.
 */

public abstract class BaseActivity<V extends ViewDataBinding> extends AppCompatActivity {

    protected V dataBinding;
    protected abstract int getLayoutId();
    private ImmersionBar mImmersionBar;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dataBinding= DataBindingUtil.setContentView(this,getLayoutId());
//        StatusBarCompat.setStatusBarColor(this, Color.parseColor("#000000"), 0);
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.statusBarColor(R.color.black).init();     //状态栏颜色，不写默认透明色.init();   //所有子类都将继承这些相同的属性
        initView(savedInstanceState);
    }

    protected void initView(@Nullable Bundle savedInstanceState) {

    }



    public void switchFragment(Fragment to){
        FragmentTransaction transaction=this.getSupportFragmentManager().beginTransaction()
                .setCustomAnimations(android.R.anim.fade_in,android.R.anim.fade_out);
        if (!to.isAdded()){
            transaction.replace(getFrameLayoutId(),to,to.getClass().getSimpleName())
                    .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                    .addToBackStack(to.getClass().getSimpleName())
                    .commit();
        }else {
            transaction.show(to).commit();
        }
    }


    //添加fragment,移除了当前的fragment，添加新fragment。点击返回键fragment会重建
    protected void addFragment(Fragment fragment) {
        if (fragment != null) {
            getSupportFragmentManager().beginTransaction()
                    .replace(getFrameLayoutId(), fragment, fragment.getClass().getSimpleName())
                    .addToBackStack(fragment.getClass().getSimpleName())
                    .commit();
        }
    }


    //加入回退栈
    public void replaceFragment(Fragment to){
        FragmentTransaction transaction=this.getSupportFragmentManager().beginTransaction()
                .setCustomAnimations(android.R.anim.fade_in,android.R.anim.fade_out);
        if (!to.isAdded()){
            transaction.replace(getFrameLayoutId(),to,to.getClass().getSimpleName())
                    .addToBackStack(to.getClass().getSimpleName())
                    .commit();
        }else {
            transaction.show(to).commit();
        }
    }

    //不加入回退栈
    public void replaceFragment2(Fragment to){
        FragmentTransaction transaction=this.getSupportFragmentManager().beginTransaction()
                .setCustomAnimations(android.R.anim.fade_in,android.R.anim.fade_out);
        if (!to.isAdded()){
            transaction.replace(getFrameLayoutId(),to,to.getClass().getSimpleName())
                    .commit();
        }else {
            transaction.show(to).commit();
        }
    }

    protected int getFrameLayoutId() {
        return 0;
    }

    public void addFragment(Fragment from, Fragment to){
        FragmentTransaction transaction=this.getSupportFragmentManager().beginTransaction()
                .setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);

        if (!to.isAdded()){
            transaction.hide(from).add(getFrameLayoutId(),to,to.getClass().getSimpleName())
                    .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                    .addToBackStack(to.getClass().getSimpleName())
                    .commit();
        } else {
            transaction.hide(from).show(to).commit();
        }

    }

    public void removeFragment(){
        if (getSupportFragmentManager().getBackStackEntryCount()>=1){
            getSupportFragmentManager().popBackStack();
        }else {
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        dataBinding.executePendingBindings();
        dataBinding.unbind();
        ImmersionBar.with(this).destroy(); //必须调用该方法，防止内存泄漏
        super.onDestroy();
    }


}
